home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Friends / Wave / WavesWorld / Source / Shaders / RCShaders / RCRound.sl < prev    next >
Encoding:
Text File  |  1995-03-22  |  1.9 KB  |  61 lines

  1. /* Listing 16.23  Displacement shader for bevelling perpendicular bilinear patches*/
  2. /* 
  3.  * round(): displace the edge of a bilinear patch so that, if it is placed next to 
  4.  *  another patch at a right angle, the edge will be rounded.
  5.  */
  6.  
  7. displacement
  8. RCRound(    
  9.     float    radius = .10 )
  10. {
  11.     float     uu,    /* distance in u to the nearest "vertical" edge */
  12.          vv,    /* distance in v to the nearest "horizontal" edge */
  13.          lu,    /* "real" distance to the nearest "vertical" edge */
  14.          lv;    /* "real" distance to the nearest "horizontal" edge */
  15.     point     center,/* point toward which the surface is displaced     */
  16.          dpdu,    /* dPdu pointed toward patch center line     */
  17.          dpdv;    /* dPdv pointed toward patch center line     */
  18.  
  19.     /* Find the distance in parameter space from the nearest edge in
  20.        u and in v, and the directions away from those edges. */
  21.     if (u < .5) {
  22.         uu = u;
  23.         dpdu = dPdu;
  24.     } else {
  25.         uu = 1 - u;
  26.         dpdu = -dPdu;
  27.     }
  28.     if (v < .5) {
  29.         vv = v;
  30.         dpdv = dPdv;
  31.     } else {
  32.         vv = 1 - v;
  33.         dpdv = -dPdv;
  34.     }
  35.  
  36.     /* Find the distances from the edges in the current space. */
  37.     lu = length(dPdu*uu);
  38.     lv = length(dPdv*vv);
  39.  
  40.     if (lu < radius || lv < radius) {                     /* only if within radius of an edge...                         */
  41.     /*
  42.      * Find the point towards which the surface  point will be 
  43.      *  moved. This center is on the center line of a cylinder, if we 
  44.      *  are not near the corner of the patch, or is the center of a 
  45.      *  sphere, if we are. We move `center' to the nearest inflection 
  46.      *  edge along u and/or v.
  47.      */
  48.         center = point(0,0,0);
  49.         if (lu < radius)
  50.             center = (radius-lu)*normalize(dpdu);
  51.         if (lv < radius)
  52.             center += (radius-lv)*normalize(dpdv);
  53.         /* Move center perpendicular to the surface */
  54.         center += P - radius*normalize(N);
  55.         /* Make P be distance 'radius' along the line 
  56.                  * from 'center' to P */
  57.         P = center + radius*normalize( P-center );
  58.     }
  59.     N = calculatenormal(P);
  60. }
  61.